const Comment = require("../../db").Comment

module.exports = {
    async find(ctx, next) {
        let id = ctx.params.id
        let pageNum = ctx.query.pageNum
        try {
            let comment = await Comment.find({ "cid":id },(err,doc)=>{
                if(!err){
                    console.log(doc)
                }
            }).skip(10 * (pageNum-1)).limit(10).sort({'_id':1}).exec()

            ctx.body = {
                code: 200,
                msg: "获取信息成功",
                list: comment
            }
        } catch(e){
            console.log(e)
            ctx.body = {
                code: 500,
                msg: "获取信息失败，服务器异常，请稍后再试",
                getErr: e
            }
        }
    },
    async add(ctx,next){
        let cid = ctx.params.id
        let { content } = ctx.request.body
        let comment = new Comment({ content: content,cid: cid })

        res = await comment.save()
        if(res._id != null){
            ctx.body = {
                code: 200,
                msg: "add success"
            }
        }
    }
}